<?php
/**
 * Created by PhpStorm.
 * User: lih
 * Date: 2018/5/19
 * Time: 21:39
 */

namespace common\models;

use common\utils\Utils;

/**
 * Class PostMedia
 * @package common\models
 * @property string $mtype
 * @property string $images
 * @property string $video
 * @property string $videocover
 * @property string $audio
 */
class PostMedia
{

    public $mtype;
    public $images;
    public $video;
    public $videocover;
    public $audio;

    const MTYPE_IMAGE = '1';
    const MTYPE_VIDEO = '2';
    const MTYPE_AUDIO = '3';

    /**
     * @return string
     */
    public function toJson()
    {
        $dataArray = array();
        if(!Utils::isEmpty($this->mtype)) {
            $dataArray['mtype'] = $this->mtype;
        }
        if(!Utils::isEmpty($this->images)) {
            $dataArray['images'] = $this->images;
        }
        if(!Utils::isEmpty($this->video)) {
            $dataArray['video'] = $this->video;
        }
        if(!Utils::isEmpty($this->videocover)) {
            $dataArray['videocover'] = $this->videocover;
        }
        if(!Utils::isEmpty($this->audio)) {
            $dataArray['audio'] = $this->audio;
        }
        if(!empty($dataArray)) {
            return json_encode($dataArray, JSON_UNESCAPED_UNICODE);
        }
        return '';
    }

    public function fromJson($strJson) {
        $dataArray = json_decode($strJson, true);
        if(!empty($dataArray)) {
            $this->mtype = @$dataArray['mtype'];
            $this->images = @$dataArray['images'];
            $this->video = @$dataArray['video'];
            $this->videocover = @$dataArray['videocover'];
            $this->audio = @$dataArray['audio'];
        }
    }

}